import json
import os
import requests
from time import sleep
def fetch_page_data(page_num, page_size,headers):
	url = f'https://cuttlefish.baidu.com/nshop/doc/getlist?sub_tab=1&pn={page_num}&rn={page_size}&query=&doc_id_str=&time_range=&buyout_show_type=1&needDayUploadUserCount=1'
	response = requests.get(url, headers=headers)
	sleep(0.5)
	print(f"获取第{page_num+1}页数据")
	if response.status_code == 200:
		return response.json()
	else:
		print(f"Failed to fetch data for page {page_num}. Status code: {response.status_code}")
		return None
def get_article(cookie_file="cookies_jdl.txt"):
	page_size = 10
	page_num = 0
	all_docs = []
	cookies_path = os.path.join(os.path.dirname(os.getcwd()), '文库登录', cookie_file)
	with open(cookies_path, "r", encoding='utf-8') as f:
		cookies_json_loaded = f.read()
	#生成headers
	cookies = json.loads(cookies_json_loaded)
	cookie_str = '; '.join([f"{cookie['name']}={cookie['value']}" for cookie in cookies])
	headers = {'Cookie': cookie_str}
	while True:
		data = fetch_page_data(page_num, page_size,headers)
		if not data or "data" not in data or "doc_list" not in data["data"]:
			break
		doc_list = data["data"]["doc_list"]
		all_docs.extend(doc_list)
		# if len(doc_list) < page_size:
		if len(doc_list) == 0:
			break
		page_num += 1
		print('已获取到文档列表')
	return all_docs

# print(get_article())
def get_headers(cookie_file="cookies_jdl.txt"):
	cookies_path = os.path.join(os.path.dirname(os.getcwd()), '文库登录', cookie_file)
	with open(cookies_path, "r", encoding='utf-8') as f:
		cookies_json_loaded = f.read()
	#生成headers
	# cookies = json.loads(cookies_json_loaded)
	# cookie_str = '; '.join([f"{cookie['name']}={cookie['value']}" for cookie in cookies])
	headers = {
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
		'Cookie': 'BIDUPSID=EFA95006EF228CB383A558544C7AA44F; PSTM=1723920339; BAIDUID=EFA95006EF228CB383A558544C7AA44F:FG=1; H_WISE_SIDS=60274_60360_60623_60629_60663_60678_60683; ZFY=SitlLL9qsAU7:AfLswzJq1vrVzMkt88Dkr7crXA1R4r4:C; BAIDUID_BFESS=EFA95006EF228CB383A558544C7AA44F:FG=1; H_WISE_SIDS_BFESS=60274_60360_60623_60629_60663_60678_60683; H_PS_PSSID=60274_60360_60629_60663_60678_60683_60573_60725; BA_HECTOR=00a0a12g2g0h8k0g0k04a524b7o73n1jdjh8s1u; ppfuid=FOCoIC3q5fKa8fgJnwzbE0LGziLN3VHbX8wfShDP6RCsfXQp/69CStRUAcn/QmhIlFDxPrAc/s5tJmCocrihdwitHd04Lvs3Nfz26Zt2holplnIKVacidp8Sue4dMTyfg65BJnOFhn1HthtSiwtygiD7piS4vjG/W9dLb1VAdqPNMaFa6COBr2OFC12lralcO0V6uxgO+hV7+7wZFfXG0MSpuMmh7GsZ4C7fF/kTgmvq/k11nkKpEvJu9aKoOwiuNqiSlcS58Ly9mjkdbS+gNuLgcrFRyrB1NUhD+vuUH5U1v2iwj13daM+9aWJ5GJCQM+RpBohGNhMcqCHhVhtXpVObaDCHgWJZH3ZrTGYHmi7XJB9z3y2o8Kqxep5XBCsugNOW5C73e/g54kuY4PKISzHuSF3RY8ms7HGCZl0sxVYdIEDXBY/JQ2gH4xEd/WFVSecr9yhMSRLVoFktEC1isNPyTb2nOKZNn5eUFPCS4G+vK/S9Ff5RtLDcahg8QCqqP/JUZA7BRBFh68uqDQax10gfXgGxCNf3Sx8e4KXUBrqV/g3hEEf9luu8oPziRIwanIJY1XZupqPZgmfh8BLwT9YUuyc0u8RKTitzO23hSwGX7sI4U3M5cfLBwVX5m74NveYUNi7Li87S8ZbXy31eyxBDK4IiDGlt1VFsxDIz0RsVHZudegSJ4zYa95fLOW41HdqdlVsa4ORVPwaoYgWzWigT4KUSvejPWWbczD37o0JAMY0Xq/mt7JbC+fPJzgUf4GCQqiHdFUsL92lNKMcIGjou59Z3egKew/5m/staUjOlIEzkOdRBF2Z6zzH6v2okHJMc4xeuRg7bfpEY/vwboa87Mf4DRxb3AAPFSzwHIQsKUb2NhurFXPH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